package com.hehe.Base;

import lombok.Getter;

/*


    MyResponse.java


 自定义返回接口封装,
 比如:    return MyResponse.ERROR("请登录");

 前端 fetch:
    .then(response => response.json())
    .then(data => {
        console.log(data.success);   // true/false
        console.log(data.message);   // 请登录, xxxxxxx
        console.log(data);
        if (data.success === false) {
           // showAlert( data.message, "failure");
            return false;
        }

返回数据:
成功:
    {
        "success": false,
        "message": "errorrrrrrrr......",
        "data": null
    }

失败:
    {
        "success": true,
        "message": "successsssss......",
        "data": null
    }

前端应该用 json 来解析:
    .then(response => response.json())      //  .then(response => response.text())
    .then(data => {
        console.log(data);
        console.log(data.success);
    })



 */
@Getter
public class MyResponse<T> {
    private final boolean success; // 状态: true=成功, false=失败
    private final String message;  // 提示信息
    private final T data;          // 可选的返回数据

    private MyResponse(boolean success, String message, T data) {
        this.success = success;
        this.message = message;
        this.data = data;
    }

    // 成功
    public static <T> MyResponse<T> SUCCESS(String message) {
        return new MyResponse<>(true, message, null);
    }

    public static <T> MyResponse<T> SUCCESS(String message, T data) {
        return new MyResponse<>(true, message, data);
    }

    // 失败
    public static <T> MyResponse<T> ERROR(String message) {
        return new MyResponse<>(false, message, null);
    }

    public static <T> MyResponse<T> ERROR(String message, T data) {
        return new MyResponse<>(false, message, data);
    }
}
